﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using RainyPWS.Business;
using RainyPWS.Common;
using RainyPWS.Model;

namespace RainyPWS.Web.Admin
{
    public partial class RecordProcess : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = MRequest.GetInt("id", 0);
            if (!IsPostBack)
            {
                lblStatus.Visible = false;
                txtSummary.Height = new Unit(45);
                ddlType_Bind();
                if (id != 0)
                {
                    MRecord record = BRecord.Instance.GetModelById(id);
                    if (record != null)
                    {
                        Data_Init(record);
                    }
                }
            }
        }

        private void Data_Init(MRecord record)
        {
            txtAuthor.Text = record.Author;
            txtKeys.Text = record.KeyWorks;
            txtSummary.Text = record.Summary;
            txtTitle.Text = record.Title;
            hdnId.Value = record.ID.ToString();
            TinyEditor.Text = record.Content;
            lblStatus.Text = RecordStatus.GetStatusName(record.Status);
            hdnStatus.Value = record.Status.ToString();
            lblStatus.Visible = true;
            txtSummary.Height = new Unit(72);
        }
        
        private void ddlType_Bind()
        {
            DataTable dt = BRecordType.Instance.GetDataType();
            ddlType.DataSource = dt;
            ddlType.DataTextField = "name";
            ddlType.DataValueField = "id";
            ddlType.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            MRecord o = new MRecord();
            o.ID = Utils.StrToInt(hdnId.Value, 0);
            o.AddedDate = DateTime.Now;
            o.Author = txtAuthor.Text.Trim();
            o.Content = TinyEditor.Text.Trim();
            o.KeyWorks = txtKeys.Text.Trim();
            o.Status = Utils.StrToInt(hdnStatus.Value, RecordStatus.Ready);
            o.Summary = txtSummary.Text.Trim();
            o.Title = txtTitle.Text.Trim();
            o.TypeID = Utils.StrToInt(ddlType.Value, 0);

            string errorMsg = string.Empty;
            if (!Validate(o, out errorMsg))
            {
                Utils.MsgBox(Page, errorMsg);
                return;
            }

            int result;
            if (o.ID != 0)
            {
                result = BRecord.Instance.UpdateData(o);
            }
            else
            {
                result = BRecord.Instance.InsertData(o);
            }
            if (result > 0)
            {
                Utils.ModalDialogClose(Page, "ok");
            }
        }

        private bool Validate(MRecord o, out string msg)
        {
            if (o.Title.Length == 0)
            {
                msg = "标题不能为空";
                return false;
            }
            if (o.TypeID == 0)
            {
                msg = "请选择记事的类型";
                return false;
            }
            msg = "";
            return true;
        }

    }
}